package cn.cnf.controller;

import cn.cnf.common.Result;
import cn.cnf.common.enums.StateEnum;
import cn.cnf.common.enums.StatusCode;
import cn.cnf.common.enums.WorkType;
import cn.cnf.common.exception.CustomException;
import cn.cnf.common.utils.UserRequestUtil;
import cn.cnf.entity.Comic;
import cn.cnf.entity.vo.ComicDetailsInfo;
import cn.cnf.service.ChapterService;
import cn.cnf.service.ComicDetailsService;
import cn.cnf.service.ComicService;
import cn.cnf.service.SubscribeService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sun.istack.internal.NotNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 服务控制器
 *
 * @author ZhuMeiYi
 * @description 漫画控制器
 * @since 2020-12-04 15:18:43
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/comic")
public class ComicController {
    @Autowired
    private ComicService comicService;

    @Autowired
    private SubscribeService subscribeService;

    @Autowired
    private ChapterService chapterService;

    @Autowired
    private ComicDetailsService comicDetailsService;

    /**
     * 全部漫画，分页
     *
     * @param current
     * @param pageSize
     * @return
     */
    @GetMapping("/list")
    public Result getComic(@RequestParam("current") Integer current, @RequestParam("pageSize") Integer pageSize) {
        try {
            return Result.success(comicService.selectPageVo(current, pageSize,null));
        } catch (CustomException e) {
            log.warn(e.getMessage());
            return Result.warn(e.getMessage());
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return Result.error(StatusCode.SYSTEM_ERROR.getMessage());
        }
    }

    @GetMapping("/queryById")
    public Result getComicById(@RequestParam("id") String id) {
        try {
            Comic comic = comicService.getById(id);
            if (comic==null){
                return Result.warn("不存在");
            }
            return Result.success(comic);
        } catch (CustomException e) {
            log.warn(e.getMessage());
            return Result.warn(e.getMessage());
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return Result.error(StatusCode.SYSTEM_ERROR.getMessage());
        }
    }

    /**
     * 搜索名字相似
     *
     * @param current
     * @param pageSize
     * @param comicName
     * @return
     */
    @GetMapping("/search")
    public Result searchComicByName(@RequestParam("current") Integer current,
                                    @RequestParam("pageSize") Integer pageSize,
                                    @RequestParam("comicName") String comicName) {
        try {
            return Result.success(comicService.searchByName(current, pageSize, comicName));
        } catch (CustomException e) {
            log.warn(e.getMessage());
            return Result.warn(e.getMessage());
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return Result.error(StatusCode.SYSTEM_ERROR.getMessage());
        }
    }

    /**
     * 章节详细
     *
     * @param comicId
     * @param chapterId
     * @return
     */
    @GetMapping("/details")
    public Result getChapterDetails(String comicId, String chapterId) {
        try {
            // 如果没有订阅
            if (!subscribeService.authority(UserRequestUtil.getCurrentUserName(), comicId, chapterId,WorkType.COMIC)) {
                return Result.warn("您没有订阅，请前往订阅或充值");
            }
            return Result.success(comicService.getDetails(comicId, chapterId));
        } catch (CustomException e) {
            log.warn(e.getMessage());
            return Result.warn(e.getMessage());
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return Result.error(StatusCode.SYSTEM_ERROR.getMessage());
        }
    }

    /**
     * 新增&更新漫画
     *
     * @param comic
     * @return
     */
    @PostMapping("/addOrUpdateComic")
    public Result addOrUpdateComic(@RequestBody Comic comic) {

        try {
            return Result.success(comicService.addOrUpdateComic(comic));
        } catch (CustomException e) {
            log.warn(e.getMessage());
            return Result.warn(e.getMessage());
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return Result.error(StatusCode.SYSTEM_ERROR.getMessage());
        }
    }


    /**
     * 新增章节详细
     *
     * @param comicDetails
     * @return
     */
    @PostMapping("/addOrUpdateDetails")
    public Result addChapterDetails(@RequestBody ComicDetailsInfo comicDetails) {
        try {
            return Result.success(comicService.addOrUpdateDetails(comicDetails));
        } catch (CustomException e) {
            log.warn(e.getMessage());
            return Result.warn(e.getMessage());
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return Result.error(StatusCode.SYSTEM_ERROR.getMessage());
        }
    }

    /**
     * 删除漫画
     *
     * @param comicId
     * @return
     */
    @GetMapping("/delete")
    public Result deleteComic(@RequestParam("comicId") String comicId) {
        try {
            // 删除章节详细
            comicDetailsService.delete(comicId);
            // 删除章节
            chapterService.delete(comicId, WorkType.COMIC);
            // 删除漫画及文件
            comicService.delete(comicId);
            return Result.success(comicId);
        } catch (CustomException e) {
            log.warn(e.getMessage());
            return Result.warn(e.getMessage());
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return Result.error(StatusCode.SYSTEM_ERROR.getMessage());
        }
    }

    /**
     * 删除漫画章节
     *
     * @param comicId
     * @param chapterId
     * @return
     */
    @GetMapping("/deleteChapter")
    public Result deleteComicChapter(@RequestParam("comicId") String comicId,
                                     @RequestParam("chapterId") String chapterId) {
        try {
            // 删除章节详细
            comicDetailsService.delete(comicId, chapterId);
            // 删除章节
            chapterService.removeById(chapterId);
            // 删除文件
            comicService.deleteChapterImg(comicId, chapterId);
            return Result.success();
        } catch (CustomException e) {
            log.warn(e.getMessage());
            return Result.warn(e.getMessage());
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return Result.error(StatusCode.SYSTEM_ERROR.getMessage());
        }
    }

    @GetMapping("/personalComic")
    public Result works(String username) {
        try {
            return Result.success(comicService.getPersonalComics(StringUtils.isBlank(username) ? UserRequestUtil.getCurrentUserName() : username));
        } catch (CustomException e) {
            log.warn(e.getMessage());
            return Result.warn(e.getMessage());
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return Result.error(StatusCode.SYSTEM_ERROR.getMessage());
        }
    }

    @GetMapping("/updateState")
    public Result updateComicState(@RequestParam("comicId")String comicId
            ,@RequestParam("state")Integer state) {
        try {
           return Result.success(comicService.updateState(comicId, StateEnum.getByValue(state)));
        } catch (CustomException e) {
            log.warn(e.getMessage());
            return Result.warn(e.getMessage());
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return Result.error(StatusCode.SYSTEM_ERROR.getMessage());
        }
    }

    /**
     * 作品关联
     *
     * @param jsonObject
     * @return
     */
    @PostMapping("/free")
    public Result linkAuthor(@RequestBody JSONObject jsonObject){
        try {
            return Result.success(comicService.linkAuthor(jsonObject));
        } catch (CustomException e) {
            log.warn(e.getMessage());
            return Result.warn(e.getMessage());
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return Result.error(StatusCode.SYSTEM_ERROR.getMessage());
        }
    }


}